package com.wxp.demo08.list;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.wxp.demo08.R;
import com.wxp.demo08.list.adapter.PlanetBaseAdapter;
import com.wxp.demo08.list.model.PlanetModel;
import com.wxp.demo08.util.PxUtils;
import com.wxp.demo08.util.ToastUtil;

import java.util.List;

/**
 * 2.2.1 列表视图ListView
 */
public class ListViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener {

    private List<PlanetModel> planetList;
    private CheckBox ck_diviver;
    private CheckBox ck_selector;
    private ListView lv_planet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_list_view);

        this.setTitle("2.2.1 列表视图ListView");

        lv_planet = findViewById(R.id.lv_planet);
        planetList = PlanetModel.getDefaultList();
        PlanetBaseAdapter adapter = new PlanetBaseAdapter(this, planetList);
        lv_planet.setAdapter(adapter);

        lv_planet.setOnItemClickListener(this);

        ck_diviver = findViewById(R.id.ck_divider);
        ck_diviver.setOnCheckedChangeListener(this);
        ck_selector = findViewById(R.id.ck_selector);
        ck_selector.setOnCheckedChangeListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ToastUtil.show(this, "您选择的是：" + planetList.get(position).name);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        //设置
        if (buttonView.getId() == R.id.ck_divider) {
            // 显示分隔线
            if (ck_diviver.isChecked()) {
                // 从资源文件获得图形对象
                Drawable drawable = this.getResources().getDrawable(R.color.black, this.getTheme());
                lv_planet.setDivider(drawable);
                // 设置列表视图的分隔线高度
                lv_planet.setDividerHeight(PxUtils.dipToPx(this, 1));
            } else {
                lv_planet.setDivider(null);
                lv_planet.setDividerHeight(0);
            }
            return;
        }

        if (buttonView.getId() == R.id.ck_selector) {
            // 显示按压背景
            if (ck_selector.isChecked()) {
                // 设置列表项的按压状态图形
                lv_planet.setSelector(R.drawable.list_selector);
            } else {
                Drawable drawable = getResources().getDrawable(R.color.transparent, getTheme());
                lv_planet.setSelector(drawable);
            }
            return;
        }

    }
}